package com.example.demo.springcycle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        logger.info("BeanDefinitionRegistryPostProcessor的postProcessBeanDefinitionRegistry方法,在这里可以增加修改删除bean的定义");
/*        final BeanDefinition webSecurityConfig = registry.getBeanDefinition("webSecurityConfig");
        final BeanDefinition webSecurityConfig2 = registry.getBeanDefinition("webSecurityConfig2");
        final String beanClassName = webSecurityConfig.getBeanClassName();
        final Class<?> aClass;
        try {
            aClass = Class.forName(beanClassName);
            final Method configure = aClass.getMethod("configure", HttpSecurity.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        registry.removeBeanDefinition("webSecurityConfig2");*/

    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        logger.info("BeanDefinitionRegistryPostProcessor的postProcessBeanFactory方法,在这里可以对beanFactory做一些操作");
    }
}
